//
// Created by love on 2022/9/12.
//

#include "pngImage.h"
#include "LogUtil.h"
#include <jni.h>

#include <EGL/egl.h>
#include <EGL/eglext.h>

EGLDisplay display;
EGLConfig config;
EGLint numConfigs;
EGLContext context;

extern "C"
JNIEXPORT void JNICALL
Java_com_xixilala_video_jni_PngImageUtil_init(JNIEnv
* env,
jobject thiz, jobject _surface) {
    pngImage *pngImage = new class pngImage();
    pngImage->init();

    // 上下文搭建，因为opengl不负责窗口及上下文环境管理，该职责由各个平台或者设备自行搭建
    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (display == EGL_NO_DISPLAY) {
        LOGE("egl return error %d",eglGetError());
        return;
    }
    if (!(eglInitialize(display,0,0))) {
        LOGE("eglInitial return error %d" , eglGetError());
        return;
    }
    const EGLint attribs[] = {EGL_BUFFER_SIZE,32,
                              EGL_ALPHA_SIZE,8,
                              EGL_BLUE_SIZE,8,
                              EGL_GREEN_SIZE,8,
                              EGL_RED_SIZE,8,
                              EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,
                              EGL_SURFACE_TYPE,EGL_WINDOW_BIT,
                              EGL_NONE};
    if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs)) {
        LOGE("eglChooseConfig return error %d",eglGetError());
        return;
    }

    EGLint attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2 , EGL_NONE};
    if (!(context = eglCreateContext(display, config, NULL, attributes))) {
        LOGE("eglCreateContext return error %s",eglGetError());
        return;
    }

    EGLSurface surface = NULL;
    EGLint format;
    if (!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format)) {
        LOGE("eglGetConfigAttrib return error %s",eglGetError());
        return;
    }

    ANativeWindow *_window = ANativeWindow_fromSurface(env,_surface);
//    ANativeWindow_setBuffersGeometry(_window,0,0,format);

}


void pngImage::init() {


}
